package edu.eece261.fall11;

import java.io.Serializable;
import java.util.LinkedList;

public class Messages {
	public enum ActionType {
		POLL, LOGOUT, CREATEUSER, LOGIN, ADDLIST, ADDITEM, JOINLIST, DELETELIST, FINDLISTS, LEAVELIST, FINISHLIST, DELETEITEM, EDITITEM
	};
	
	public enum ResponseType {
		SUCCESS, FAILURE, ONHOLD, FAILURENOTLOGGEDIN, FAILURENOTPERMITTED, FAILUREEXISTS, FAILUREFINISHED, FAILUREUSER, FAILUREPASS
	};

	public static class ShopList implements Serializable {

		private static final long serialVersionUID = -8492124912807072971L;
		
		private long global_id;
		private long timestamp;
		private String title;
		private String owner;
		private LinkedList<String> items;
		private LinkedList<String> updaters;
		private LinkedList<Long> timestamps;
		private LinkedList<String> followers;
		private String lastUpdater;
		private String password = "";
		private int isPrivate = 0;
		private int finished;
		
		public ShopList() {
			items = new LinkedList<String>();
			updaters = new LinkedList<String>();
			timestamps = new LinkedList<Long>();
			followers = new LinkedList<String>();
		};
		
		public long getGlobal_id() {
			return global_id;
		}
		public ShopList setGlobal_id(long l) {
			global_id = l;
			return this;
		}
		public String getTitle() {
			return title;
		}
		public ShopList setTitle(String title) {
			this.title = title;
			return this;
		}
		public long getTimestamp() {
			return timestamp;
		}

		public ShopList setTimestamp(long timestamp) {
			this.timestamp = timestamp;
			return this;
		}
		public String getOwner() {
			return owner;
		}
		public ShopList setOwner(String owner) {
			this.owner = owner;
			return this;
		}
		public ShopList setItems(LinkedList<String> items) {
			this.items = items;
			return this;
		}

		public LinkedList<String> getItems() {
			return items;
		}
		
		public ShopList addItem(String item, String updater, long time) {
			this.items.add(item);
			this.updaters.add(updater);
			this.timestamps.add(time);
			this.addFollower(updater);
			this.lastUpdater = updater;
			return this;
		}
		
		public ShopList setUpdaters(LinkedList<String> updaters) {
			this.updaters = updaters;
			return this;
		}
		
		public LinkedList<String> getUpdaters() {
			return updaters;
		}
		
		public ShopList setTimestamps(LinkedList<Long> timestamps) {
			this.timestamps = timestamps;
			return this;
		}
		
		public LinkedList<Long> getTimestamps() {
			return timestamps;
		}

		public ShopList setFollowers(LinkedList<String> followers) {
			this.followers = followers;
			return this;
		}

		public LinkedList<String> getFollowers() {
			return followers;
		}
		
		public ShopList addFollower(String follower) {
			if (!this.followers.contains(follower)) {
				followers.add(follower);
			}
			return this;
		}

		public ShopList setLastUpdater(String lastUpdater) {
			this.lastUpdater = lastUpdater;
			return this;
		}

		public String getLastUpdater() {
			return lastUpdater;
		}

		public ShopList setFinished(int finished) {
			this.finished = finished;
			return this;
		}

		public int isFinished() {
			return finished;
		}

		public int getIsPrivate() {
		return isPrivate;
		}
		
		public ShopList setIsPrivate(int isPrivate) {
			this.isPrivate = isPrivate;
			return this;
		}
		
		public String getPassword() {
			return password;
		}
		
		public ShopList setPassword(String password) {
			this.password = password;
			return this;
		}
	};
	
	public static class MessageToClient implements Serializable {
		private static final long serialVersionUID = 2926640678354928356L;
		
		private ResponseType response;
		private ActionType action = ActionType.POLL;
		private long lastModified;
		private LinkedList<Messages.ShopList> lists;
		private String username;
		private String uuid;
		
		public MessageToClient() {
			lists = new LinkedList<Messages.ShopList>();
		}

		public ResponseType getResponse() {
			return response;
		}

		public MessageToClient setResponse(ResponseType response) {
			this.response = response;
			return this;
		}
		
		public ActionType getAction() {
			return action;
		}

		public MessageToClient setAction(ActionType action) {
			this.action = action;
			return this;
		}

		public long getLastModified() {
			return lastModified;
		}

		public MessageToClient setLastModified(long lastModified) {
			this.lastModified = lastModified;
			return this;
		}

		public LinkedList<Messages.ShopList> getLists() {
			return lists;
		}

		public MessageToClient setLists(LinkedList<Messages.ShopList> lists) {
			this.lists = lists;
			return this;
		}

		public String getUsername() {
			return username;
		}

		public MessageToClient setUsername(String username) {
			this.username = username;
			return this;
		}

		public String getUuid() {
			return uuid;
		}

		public MessageToClient setUuid(String uuid) {
			this.uuid = uuid;
			return this;
		}
		
		public MessageToClient addList(Messages.ShopList lst)
		{
			this.lists.add(lst);
			return this;
		}
	}
	
	public static class MessageToServer implements Serializable {		
		private static final long serialVersionUID = 9163944436536608171L;
		
		private ResponseType response;
		private ActionType action = ActionType.POLL;
		private String username;
		private String password;
		private String name;
		private String email;
		private String title;
		private String item;
		private String listPassword;
		private long itemTimestamp;
		private String uuid;
		private int offset;
		private long lastModifiedTime;
		
		public MessageToServer() {
		}

		public ResponseType getResponse() {
			return response;
		}

		public MessageToServer setResponse(ResponseType response) {
			this.response = response;
			return this;
		}

		public ActionType getAction() {
			return action;
		}

		public MessageToServer setAction(ActionType action) {
			this.action = action;
			return this;
		}

		public String getUsername() {
			return username;
		}

		public MessageToServer setUsername(String username) {
			this.username = username;
			return this;
		}
		
		public String getPassword() {
			return password;
		}
		
		public MessageToServer setPassword(String password) {
			this.password = password;
			return this;
		}

		public String getName() {
			return name;
		}
		
		public MessageToServer setName(String name) {
			this.name = name;
			return this;
		}
		
		public String getEmail() {
			return email;
		}
		
		public MessageToServer setEmail(String email) {
			this.email = email;
			return this;
		}

		public String getTitle() {
		 	return title;
		}

		public MessageToServer setTitle(String title) {
			this.title = title;
			return this;
		}
		
		public String getItem() {
			return item;
		}
		
		public MessageToServer setItem(String item) {
			this.item = item;
			return this;
		}
		
		public String getListPassword() {
			return listPassword;
		}
		
		public MessageToServer setListPassword(String listPassword) {
			this.listPassword = listPassword;
			return this;
		}
		
		public String getUuid() {
			return uuid;
		}

		public MessageToServer setUuid(String uuid) {
			this.uuid = uuid;
			return this;
		}

		public long getLastModifiedTime() {
			return lastModifiedTime;
		}

		public MessageToServer setLastModifiedTime(long lastModifiedTime) {
			this.lastModifiedTime = lastModifiedTime;
			return this;
		}
		
		public int getOffset() {
			return offset;
		}
		
		public MessageToServer setOffset(int offset) {
			this.offset = offset;
			return this;
		}

		public long getItemTimestamp() {
			return itemTimestamp;
		}
		
		public MessageToServer setItemTimestamp(long itemTimestamp) {
			this.itemTimestamp = itemTimestamp;
			return this;
		}
	}
}